package com.glp.work.flow.demo.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.glp.common.reponse.BaseResult;
import com.glp.work.flow.client.WfProcessInitiateClient;
import com.glp.work.flow.constant.APICode;
import com.glp.work.flow.demo.service.UserDemoService;
import com.glp.work.flow.exception.APIException;
import com.glp.work.flow.mapper.UserDemoMapper;
import com.glp.work.flow.pojo.UserDemo;
import com.glp.work.flow.request.ProcessInitiateRequest;
import com.glp.work.flow.request.WfOrderQueryDTO;
import com.glp.work.flow.response.ProcessInitiateResponse;
import com.glp.work.flow.response.WfOrderResultDTO;
import com.glp.work.flow.service.WfProcessQueryService;
import com.glp.work.flow.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName UserDemoServiceImpl
 * @Description: TODO
 * @Author ywguo
 * @Date 2020/6/2 15:01
 * @Version V1.0
 **/

@Service("userDemoServiceImpl")
@Slf4j
class UserDemoServiceImpl implements UserDemoService {

    @Autowired
    private UserDemoMapper userDemoMapper;

    @Autowired
    private WfProcessInitiateClient wfProcessInitiateClient;

    @Autowired
    private WfProcessQueryService wfProcessQueryService;

    /**
     * 代办列表
     *
     * @MethodName: userDemoAgency
     * @Description: TODO
     * @Param: []
     * @Return: com.glp.common.reponse.BaseResult
     * @Author: ywguo
     * @Date: 2020/6/2
     **/
    @Override
    public PageInfo<WfOrderResultDTO> userDemoAgencyList(WfOrderQueryDTO wfOrderQueryDTO) {
        log.info("查询待办工单,请求参数:{}", JSON.toJSONString(wfOrderQueryDTO));
        if (StringUtils.isBlank(wfOrderQueryDTO.getUserNo())) {
            throw new APIException(APICode.DATA_ERROR, "用户编码不能为空！");
        }
        log.info("用户信息userId:{}", wfOrderQueryDTO);
        return wfProcessQueryService.queryToDoTasks(wfOrderQueryDTO);
    }

    /**
     * 已办
     *
     * @MethodName: userDemoDone
     * @Description: TODO
     * @Param: []
     * @Return: java.util.List<com.glp.work.flow.pojo.UserDemo>
     * @Author: ywguo
     * @Date: 2020/6/3
     **/
    @Override
    public PageInfo<WfOrderResultDTO> userDemoDone(WfOrderQueryDTO wfOrderQueryDTO) {
        log.info("查询已办工单,请求参数:{}", JSON.toJSONString(wfOrderQueryDTO));
        if (StringUtils.isBlank(wfOrderQueryDTO.getUserNo())) {
            throw new APIException(APICode.DATA_ERROR, "用户编码不能为空！");
        }
        log.info("用户信息userId:{}", wfOrderQueryDTO);
        return wfProcessQueryService.queryDoneTasks(wfOrderQueryDTO);

    }


    @Override
    public String inserUserDemo(UserDemo userDemo) {

        ProcessInitiateRequest processInitiateRequest = new ProcessInitiateRequest();
        processInitiateRequest.setAppId(userDemo.getAppId().toString());
        processInitiateRequest.setOrderId(userDemo.getOrderId().toString());
        processInitiateRequest.setUserId(userDemo.getUserId());
        processInitiateRequest.setUserName(userDemo.getUserName());
        processInitiateRequest.setOrderTitle(userDemo.getOrderTitle());
        processInitiateRequest.setProcDefKey(userDemo.getProDefKey());
        processInitiateRequest.setProcessVariable(userDemo.getProcessVariable());
        processInitiateRequest.setDynamicForm(userDemo.getDynamicForm());

        //获取processId
        BaseResult<ProcessInitiateResponse> data = wfProcessInitiateClient.processInitate(processInitiateRequest);

        ProcessInitiateResponse processInitiateResponse = data.getData();
        if (null == processInitiateResponse || processInitiateResponse.getProcessId() == null) {
            throw new APIException(APICode.ERROR, "工作流引擎启动失败！！");
        }
        log.info("发起工作流成功:{}", data.getData());
        String processId = processInitiateResponse.getProcessId();
        //添加UserDemo
        userDemo.setProcessId(processId);
        userDemo.setStatus("待审批");
        userDemo.setSubmutTime(DateUtils.getCurrent());
        log.info("添加工单demo数据:{}", userDemo);
        userDemoMapper.insertSelective(userDemo);
        return processId;
    }


}
